package com.syxx.oa.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.syxx.oa.Globals.My_Globals;
import com.syxx.oa.domain.Department;
import com.syxx.oa.domain.EmpLevel;
import com.syxx.oa.domain.Employee;
import com.syxx.oa.service.EmployeeService;
import com.syxx.oa.utils.StringUtils;
import com.test.jbpm.Pager;
import com.test.jbpm.web.action.BaseAction;

@Controller
@Scope("prototype")
public class EmployeeAction extends BaseAction {
	private EmployeeService employeeService;
	private Employee emp;
	private Long[] empIds;

	public Long[] getEmpIds() {
		return empIds;
	}



	public void setEmpIds(Long[] empIds) {
		this.empIds = empIds;
	}



	public Employee getEmp() {
		return emp;
	}



	public void setEmp(Employee emp) {
		this.emp = emp;
	}



	@Autowired
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	public String addOrUpdate(){
		String retStr = "";
		try {
			employeeService.addOrUpdate(emp);
			retStr = ajaxOut("{success: true}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retStr;
	}
	
	public String index() throws Exception {
		return SUCCESS;
	}
	
	public String list() throws Exception {
		Pager<Employee> pager = employeeService.findEmpList();
		StringBuffer sbJson = new StringBuffer();
		sbJson.append("{");
		sbJson.append("total:");
		sbJson.append(pager.getTotal());
		sbJson.append(", data:");
		List<Employee> empList = pager.getData();
		
		JSONObject jsob;
		Map<String,Object> map;
		JSONArray list = new JSONArray();
		for (Employee em : empList) {
			map = new HashMap<String, Object>();
			if(em.getDepartment() != null){
				map.put("depId", em.getDepartment().getDepId());
				map.put("depName", em.getDepartment().getDepName());
				em.setDepartment(null);
			}
			if(em.getEmpLevel() != null){
				map.put("levId", em.getEmpLevel().getLevId());
				map.put("levName", em.getEmpLevel().getLevName());
				em.setEmpLevel(null);
			}
			jsob = JSONObject.fromObject(em);
			if(!map.isEmpty()){
				jsob.putAll(map);	
			}
			
			list.add(jsob);
		}
		sbJson.append(list);
		sbJson.append("}");
		System.out.println(sbJson);
        return ajaxOut(sbJson.toString());
	}
	
	public String edit() throws Exception {
		
		System.out.println("emp.getEmpId()="+emp.getEmpId());
		emp = employeeService.findEmpById(emp.getEmpId());
		/*Map<String,Object> map = new HashMap<String, Object>();
		if(emp != null && emp.getDepartment() != null){
			Department dep = new Department();
			dep.setDepCode(emp.getDepartment().getDepCode());
			dep.setDepId(emp.getDepartment().getDepId());
			dep.setDepName(emp.getDepartment().getDepName());
			map.put("depId", emp.getDepartment().getDepId());
			map.put("depName", emp.getDepartment().getDepName());
			emp.setDepartment(dep);
			
		}
		if(emp != null && emp.getEmpLevel() != null){
//			EmpLevel lev = new EmpLevel();
//			lev.setLevCode(emp.getEmpLevel().getLevCode());
//			lev.setLevId(emp.getEmpLevel().getLevId());
//			lev.setLevName(emp.getEmpLevel().getLevName());
			emp.setEmpLevel(null);
		}*/
		long depId=emp.getDepartment().getDepId();
		long levId=emp.getEmpLevel().getLevId();
		emp.setDepartment(null);
		emp.setEmpLevel(null);
		
		JSONObject obj = StringUtils.bean2Json(emp);
		obj.put("depId", depId);
		obj.put("levId", levId);
	
		//测试
		System.out.println("employee json:"+obj);
		
		return ajaxOut("{success:true, data:" + obj + "}");
	}
	
	public String del() throws Exception{
		employeeService.del(empIds);
		return ajaxOut();
	}
	
	public String login() throws Exception {
		emp = employeeService.login(emp.getEmpLoginName(), emp.getEmpPassword());
		if (emp != null) {
			getSession().setAttribute(My_Globals.CURRENT_USER, emp);
			return ajaxOut("{success: true}");
		}else {
			return ajaxOut("{success: false}");
		}
	}
	
	public static void main(String[] args) {
//		Employee emp = new Employee();
//		emp.setEmpId("111");
//		
//		Department de = new Department();
//		de.setDepName("222");
//		emp.setDepartment(de);
//		
//		System.out.println(StringUtils.bean2Json(emp));
	}
	
}
